
/*========================================================================================
 *                                                                                      *
 *               Copyright(c) 2013-2014, Cha SangWoo, All rights reserved.              *
 *                                                                                      *
 * This file is part of ChaOS.                                                          *
 *                                                                                      *
 * Permission to use, copy, modify, and distribute this software in source and binary   *
 * forms and its documentation for any purpose and without fee is hereby granted,       *
 * provided that the above copyright notice appear in all copies and that both that     *
 * copyright notice and this permission notice appear in supporting documentation.      *
 *                                                                                      *
 * THIS SOFTWARE IS PROVIDED BY THE CHA SANGWOO AND CONTRIBUTORS "AS IS" AND ANY        *
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED            *
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   *
 * IN NO EVENT SHALL CHA SANGWOO OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,    *
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT         *
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,          *
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF            *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE      *
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,                    *
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                   *
 *                                                                                      *
========================================================================================*/

#include "co_core.h"
#include "co_system.h"
#include "co_thread.h"

#ifdef __cplusplus
    extern "C" {
    namespace ChaOS {
#endif

//////////////////////////////////////  < BEGIN >  ///////////////////////////////////////

/*======================================================================================*/
/*!
    @fn             VOID co_chaos_init(VOID)

    @brief          

    @return         None.

    - HISTORY -
    [V00.00.01] 2014-03-23 [aireat] - Created.
*/
/*======================================================================================*/
VOID co_chaos_init(VOID)
{
    OBJECT    objIdle;

    _g_nested_count = 0;
    _g_system_tick  = 0;

    _gst_heap_info.pcur = NULL;
    _gst_heap_info.pmax = NULL;

    // initialize porting
    _port_chaos_init();

    _port_log_printf("SYSTEM", "START\n");

    ////////////////////
    CO_ENTER_CRITICAL();

    // create idle thread
    objIdle = co_thread_create(SIZE_IDLE_THREAD_STACK,  // stack size
                               _idle_proc_,             // entry point
                               0);                      // argument
    co_thread_set_name(objIdle, "idle");

    // set initial value
    _gp_idle_thread = (THREAD_CB*) objIdle;
    _gp_cur_thread  = (THREAD_CB*) objIdle;

    _gp_ready_list  = NULL;
    _gp_block_list  = NULL;
    _gp_ready_list  = NULL;
}

/*======================================================================================*/
/*
    @fn             VOID co_chaos_start(VOID)

    @brief          

    @return         None.

    - HISTORY -
    [V00.00.01] 2014-03-23 [aireat] - Created.
*/
/*======================================================================================*/
VOID co_chaos_start(VOID)
{
    CO_LEAVE_CRITICAL();
    ////////////////////

    // get first thread
    _core_schedule_thread();

    // Start ChaOS
    _port_chaos_start();
}

/*======================================================================================*/
/*
    @fn             UINT32 co_get_tick_count(VOID)

    @brief          Get current Tick Count, in ms

    @return -       Tick count
*/
/*======================================================================================*/
UINT32 co_get_tick_count(VOID)
{
    return _g_system_tick;
}

/*======================================================================================*/
/*
    @fn             UINT co_version(VOID)

    @brief          

    @return -       version number

    - HISTORY -
    [V00.00.01] 2014-03-23 [aireat] - Created.
*/
/*======================================================================================*/
UINT co_version(void)
{
    return 0x00000001;
}

//////////////////////////////////////  <  END  >  ///////////////////////////////////////

#ifdef __cplusplus
    } /* namespace ChaOS */
    } /* extern "C" */
#endif

